package com.demo.controller;

import java.util.List;
import java.util.Map;

import com.demo.service.RestaurantsService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.demo.domain.Restaurants;


@EnableAutoConfiguration
@RestController
@RequestMapping(value="/rest")
public class MyRestController extends BaseRestController{
	private Logger logger = LoggerFactory.getLogger(MyRestController.class);
	
	@Autowired
	private RestaurantsService restaurantsService;
	
	@RequestMapping(value="/all",method=RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	public Object queryAll() {
		logger.info("===========queryAll()  start=========");
		Map<String,Object> result = initialMessage();
		long startTime = System.currentTimeMillis();
		List<Restaurants> list =restaurantsService.findAll();
		result.put("data", list);
		long cost = System.currentTimeMillis() - startTime;
		logger.info("===========queryAll() end.cost:{}=============",cost );
		return result;
	}
	
	@RequestMapping(value="/pages",method=RequestMethod.POST)
	@ResponseStatus(HttpStatus.OK)
	public Object queryPage(@RequestParam int start,@RequestParam int count){
		logger.info("===========queryPage()  start=========");
		Map<String,Object> result = initialMessage();
		long startTime = System.currentTimeMillis();
		List<Restaurants> list =restaurantsService.findAllPaging(start, count);
		result.put("data", list);
		long cost = System.currentTimeMillis() - startTime;
		logger.info("===========queryPage() end. count:{}, cost:{} =============",list.size(), cost);
		return result;
	}
	
	@RequestMapping(value="/queryByName",method=RequestMethod.POST)
	@ResponseStatus(HttpStatus.OK)
	public Object queryByName(@RequestParam String name) {
		logger.info("===========queryByName() start,paramter:  {name:"+name+"}");
		logger.info("");
		Map<String,Object> result = initialMessage();
		if (StringUtils.isEmpty(name)) {
			provcessError(result, 200, "参数name不能为空");
			return result;
		}
		List<Restaurants> list =restaurantsService.findByName(name);
		result.put("data", list);
		logger.info("===========queryByName() end=============");
		return result;
	}
	
	@RequestMapping(value="/queryById",method=RequestMethod.POST)
	@ResponseStatus(HttpStatus.OK)
	public Object queryById(@RequestParam String id) {
		logger.info("===========queryById() start,paramter:  {id:"+id+"}");
		Map<String,Object> result = initialMessage();
		if (StringUtils.isEmpty(id)) {
			provcessError(result, 200, "参数id不能为空");
			return result;
		}
		Restaurants res =restaurantsService.findById(id);
		result.put("data", res);
		logger.info("===========queryById() end=============");
		return result;
	}
	
	
	@RequestMapping(value="/queryByCuisine",method=RequestMethod.POST)
	@ResponseStatus(HttpStatus.OK)
	public Object queryByCuisine(@RequestParam String cuisine) {
		logger.info("===========queryByCuisine() start,paramter:  {cuisine:"+cuisine+"}");
		Map<String,Object> result = initialMessage();
		if (StringUtils.isEmpty(cuisine)) {
			provcessError(result, 200, "参数cuisine不能为空");
			return result;
		}
		List<Restaurants> list =restaurantsService.fingByCuisine(cuisine);
		result.put("data", list);
		logger.info("===========queryByCuisine() end=============");
		return result;
	}
	
	@RequestMapping(value="/queryByBorough",method=RequestMethod.POST)
	@ResponseStatus(HttpStatus.OK)
	public Object queryByBorough(@RequestParam String borough) {
		logger.info("===========queryByBorough() start,paramter:  {borough:"+borough+"}");
		Map<String,Object> result = initialMessage();
		if (StringUtils.isEmpty(borough)) {
			provcessError(result, 200, "参数borough不能为空");
			return result;
		}
		List<Restaurants> list =restaurantsService.findByBorough(borough);
		result.put("data", list);
		logger.info("===========queryByBorough() end=============");
		return result;
	}
	
}
